/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sybel;

import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author Stefano
 */
public class DomModel extends AbstractTreeModel implements TreeModel{
    private Node root;
    

    public DomModel(Node dom){
        root = dom;
    }

    public Object getRoot() {
        return root;
    }

    public Object getChild(Object parent, int index) {
        Node node = (Node)parent;
        return node.getChildNodes().item(index);
    }

    public int getChildCount(Object parent) {
        Node node = (Node)parent;
        NodeList childs = node.getChildNodes();
        return childs.getLength();
    }

    public boolean isLeaf(Object node) {
        Node leaf = (Node)node;
        if(leaf.hasChildNodes())
            return false;
        return true;
    }

    public void valueForPathChanged(TreePath path, Object newValue) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int getIndexOfChild(Object parent, Object child) {
        Node father = (Node)parent;
        Node son = (Node)child;
        NodeList childnodes = father.getChildNodes();
        for (int i = 0; i < childnodes.getLength(); i++){
            if(childnodes.item(i) == son)
                return i;
        }
        return -1;
    }
   
}
